package com.example.gc.webredisgc.service;

import com.allqj.qjf.multi.datasource.utils.TenantContextHolder;
import com.example.gc.webredisgc.dao.MisSaas;
import com.example.gc.webredisgc.dao.MisSaasDao;
import com.example.gc.webredisgc.dao.MisSaasES;
import com.example.gc.webredisgc.dao.MisSaasEsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author sjw
 * @className HomeService
 * @date 2021/9/22 17:16
 * @description: TODO
 */
@Service
public class HomeService {
    @Autowired
    private MisSaasDao dao;


    @Autowired
    private MisSaasEsDao esDao;



    @Autowired
    private HomeService1 homeService1;





    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public Boolean update(String name) throws InterruptedException {
        Optional<MisSaas> misSaas = dao.findById(1);
        if(misSaas.isPresent()){
            MisSaas a=misSaas.get();
            a.setName(a.getName()+name);
        }
        Thread.sleep(10000);
        MisSaas a=new MisSaas();
        a.setName(name);
        dao.save(a);
        jdbcTemplate.update("update mis_saas set name='jdbcTemplate' where id=2");
        return true;
    }



    public String es(String name) {
        MisSaasES misSaasES=new MisSaasES();
        misSaasES.setName(name);
        esDao.save(misSaasES);

        MisSaasES  e=esDao.findAllById("AXxPYpf3DymeRm_WX7uZ");
        if(e!=null) {
            e.setName("aaaa");
            esDao.save(e);
        }

        return misSaasES.getId();
    }
    @Async
    public void async() throws InterruptedException {
        Thread.sleep(1000);
        homeService1.async();
        System.out.println("async: "+ TenantContextHolder.getTenantContext());
    }



    @Async
    public void async1() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("async1: "+ TenantContextHolder.getTenantContext());
    }
}
